package com.joy.web.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.joy.common.exception.JsonException;
import com.joy.common.exception.PageException;
import com.joy.common.model.ApiResponse;
import com.joy.common.model.DateTablePage;
import com.joy.common.model.vo.ResourceVO;
import com.joy.common.model.vo.RoleVO;
import com.joy.web.admin.service.IResourceService;
import com.joy.web.admin.service.IRoleResourceService;
import com.joy.web.admin.service.IRoleService;
import com.joy.web.admin.service.IUserService;
import com.joy.common.enums.Status;
import com.joy.web.admin.entity.Role;
import com.joy.web.base.controller.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjzhao
 * @since 2019-09-16
 */
@Slf4j
@Controller
@RequestMapping("/admin/role")
public class RoleController extends BaseController {
    @Resource
    private IRoleService roleService;
    @Resource
    private IResourceService resourceService;
    @Resource
    private IRoleResourceService roleResourceService;
    @Resource
    private IUserService userService;

    /**
     * 用户管理初始化页面
     * @return
     */
    @GetMapping(value = {"", "/", "/index" })
    public String index() {
        return "pages/admin/role/index";
    }

    /**
     * 查询集合
     * @return
     */
    @GetMapping(value = { "/pagination" })
    @ResponseBody
    public DateTablePage pagination(@ModelAttribute RoleVO roleVO) {
        try {
            IPage<Role> rtnPage = roleService.findRoleListPage(this.getDataTablePage(), roleVO);
            return getDataTableRtn(rtnPage);
        } catch (Exception e) {
            log.info("/admin/role/pagination MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "获取角色集合报错了");
        }
    }

    /**
     * 编辑
     * @return
     */
    @GetMapping(value = { "/add" })
    public String add(ModelMap map) {
        try {
            List<ResourceVO> resourceVOList = resourceService.getResourceTree();
            map.addAttribute("tree", resourceVOList);
            return "pages/admin/role/add";
        } catch (Exception e) {
            log.info("/admin/role/add MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "获取获取权限树报错了");
        }

    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/addSave" })
    @ResponseBody
    public ApiResponse addSave(@ModelAttribute("role") RoleVO role) {

        try {
            //更新用户详情
            boolean upSucc = roleService.save(role);
            userService.removeUserAuthorization();
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/role/addSave MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "新增角色报错了");
        }

    }

    /**
     * 编辑
     * @return
     */
    @GetMapping(value = { "/edit/{id}" })
    public String edit(@PathVariable(value="id") String id, ModelMap map) {
        try {
            // 根据 userId 获取 用户详情
            Role role = roleService.getById(id);
            //传至前端
            map.put("role", role);
            List<ResourceVO> resourceVOList = resourceService.getResourceTree();
            map.addAttribute("tree", resourceVOList);

            List<String> resourceIdListByRoleId = roleResourceService.findResourceIdListByRoleId(role.getId());
            map.addAttribute("rList", resourceIdListByRoleId);
            //用户编辑页
            return "pages/admin/role/edit";
        } catch (Exception e) {
            log.info("/admin/role/edit MyException:");
            e.printStackTrace();
            throw new PageException(Status.UNKNOWN_ERROR, "获取角色信息报错了");
        }
    }

    /**
     * 编辑
     * @return
     */
    @PostMapping(value = { "/update" })
    @ResponseBody
    public ApiResponse update(@ModelAttribute("role") RoleVO role) {

        try {
            //更新用户详情
            boolean upSucc = roleService.updateRole(role);
            userService.removeUserAuthorization();
            return ApiResponse.ofSuccess("success");
        } catch (Exception e) {
            log.info("/admin/role/d MyException:");
            e.printStackTrace();
            throw new JsonException(Status.UNKNOWN_ERROR, "更新角色信息报错了");
        }
    }
}
